package com.sais.manage.boot.daily.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.daily.dto.ApplicationDTO;
import com.sais.manage.boot.daily.dto.ApplicationSaveDTO;
import com.sais.manage.boot.daily.dto.ApplicationSearchDTO;
import com.sais.manage.boot.daily.service.IApplicationService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 宿舍管理系统-申请 controller 类
 *
 * @author zwp
 * @date 2020/2/2
 **/
@Api(tags = "申请管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/daily/application")
public class ApplicationController {

    @Resource
    private IApplicationService applicationService;

    @ApiOperation("申请 保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveApplication(@RequestBody ApplicationSaveDTO applicationSaveDTO) {
        String userId = ShiroUtils.getLoginUser().getUkId();
        applicationSaveDTO.setUserId(userId);
        applicationService.saveApplication(applicationSaveDTO);
        return new Result();
    }

    @ApiOperation("申请 删除")
    @RequestMapping(value = "/delete/{applicationId}", method = RequestMethod.GET)
    public Result deleteApplication(@PathVariable("applicationId") String applicationId) {
        applicationService.deleteApplication(applicationId);
        return new Result();
    }

    @ApiOperation("根据id 查询 申请")
    @RequestMapping(value = "/select/{applicationId}", method = RequestMethod.GET)
    public Result selectApplication(@PathVariable("applicationId") String applicationId) {
        ApplicationDTO application = applicationService.selectApplication(applicationId);
        return new Result().data("application", application);
    }

    @ApiOperation("分页条件 查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listApplication(@RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "30") Integer size,
                                  ApplicationSearchDTO searchDTO) {
        Page<ApplicationDTO> applications = applicationService.listApplication(current, size, searchDTO);
        return new Result().data("applications", applications);
    }

    @ApiOperation("查询 当前用户管理宿舍楼的所有申请")
    @RequestMapping(value = "/list/manage", method = RequestMethod.GET)
    public Result listApplicationByManage(@RequestParam(defaultValue = "1") Integer current,
                                          @RequestParam(defaultValue = "30") Integer size,
                                          ApplicationSearchDTO searchDTO) {
        String userId = ShiroUtils.getLoginUser().getUkId();
        Page<ApplicationDTO> applications = applicationService.listApplicationByManager(userId, current, size, searchDTO);
        return Result.response().data("applications", applications);
    }

    @ApiOperation("更改申请 状态为通过")
    @RequestMapping(value = "/update/status/accept/{applicationId}", method = RequestMethod.GET)
    public Result updateApplicationAcceptStatus(@PathVariable("applicationId") String applicationId) {
        applicationService.updateApplicationAcceptStatus(applicationId);
        return Result.response();
    }

    @ApiOperation("更改申请 状态为未通过")
    @RequestMapping(value = "/update/status/refuse/{applicationId}", method = RequestMethod.GET)
    public Result updateApplicationRefuseStatus(@PathVariable("applicationId") String applicationId) {
        applicationService.updateApplicationRefuseStatus(applicationId);
        return Result.response();
    }

    @ApiOperation("根据userId 查询该用户发出的申请")
    @RequestMapping(value = "/list/user", method = RequestMethod.GET)
    public Result listApplicationByUserId() {
        String userId = ShiroUtils.getLoginUser().getUkId();
        List<ApplicationDTO> applications = applicationService.listApplicationByUserId(userId);
        return Result.response().data("applications", applications);
    }

}
